//
//  JGAnchorDigestVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/22.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "JGAnchorDigestVC.h"
#import "ListModel.h"
#import "JGOnlyListenTableViewCell.h"

const CGFloat BackGroupHeight1 = 230;


@interface JGAnchorDigestVC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableView;/**< 底层tableview */
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) ListModel *model;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) AMBlurView *blurView;
@property (nonatomic, assign) BOOL isUpStata;/**< 标记上拉刷新 */

@end

@implementation JGAnchorDigestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.model = [[ListModel alloc] init];
    self.modelArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight1, 0, 0, 0);
    [self.view addSubview:_tableView];
    _tableView.rowHeight = 90;
    
    self.isUpStata = YES;
    
    [self dataHanderTableV];
    [self dataHanger];
    [self addFooter];
    
    
    // 注册重用池
    [_tableView registerClass:[JGOnlyListenTableViewCell class] forCellReuseIdentifier:@"JGOnlyListenTableViewCell"];
    
}
- (void)createImageV{
    
    // 设置大图片
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight1, self.view.frame.size.width, BackGroupHeight1)];
//    [_imgView sd_setImageWithURL:[NSURL URLWithString:self.model.backgroundLogo]];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [_imgView HANDLEIMAGE(self.model.backgroundLogo)];
    });
    [_tableView addSubview:_imgView];
    [self setBlurView:[AMBlurView new]];
    [[self blurView] setFrame:[self.imgView bounds]];
    [[self blurView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [self.imgView addSubview:_blurView];
    self.blurView.alpha = 0.5;
    self.blurView.blurTintColor = [UIColor grayColor];
    
    // 添加头像
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight1, SCREEN_WIDTH, BackGroupHeight1)];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView addSubview:view];
    
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 65, 100, 100)];
//    [headView sd_setImageWithURL:[NSURL URLWithString:self.model.mobileSmallLogo]];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [headView HANDLEIMAGE(self.model.mobileSmallLogo)];
    });
    headView.layer.cornerRadius = 50;
    [view addSubview:headView];
    // 添加主播名
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, SCREEN_WIDTH, 30)];
    NSString *str = [NSString stringWithFormat:@"%@ ✨", self.model.nickname];
    nameL.text = str;
    nameL.font = [UIFont systemFontOfSize:20];
    nameL.textColor = [UIColor whiteColor];
    nameL.textAlignment = NSTextAlignmentCenter;
    
    [view addSubview:nameL];
    // 介绍
    UILabel *degistL = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 20)];
    degistL.text = self.model.personDescribe;
    degistL.font = [UIFont systemFontOfSize:14];
    degistL.numberOfLines = 2;
    degistL.textColor = [UIColor whiteColor];
    degistL.textAlignment = NSTextAlignmentCenter;
    [view addSubview:degistL];
    
    
    
}
#pragma mark - tableview的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JGOnlyListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JGOnlyListenTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.modelArr[indexPath.row];
    
    return cell;
}
// cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WJCollectionViewController *WJVC = [[WJCollectionViewController alloc] init];
    ListModel *model = [[ListModel alloc] init];
    model = self.modelArr[indexPath.row];
    WJVC.str = model.albumId;
    [self.navigationController pushViewController:WJVC animated:YES];

    
}
#pragma mark - 滑动改变bar的透明度
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat yOffset = scrollView.contentOffset.y;
    
    if (yOffset < -BackGroupHeight1) {
        CGRect rect = _imgView.frame;
        rect.origin.y = yOffset;
        rect.size.height = -yOffset;
        
        _imgView.frame = rect;
    }
    
    CGFloat alpha = (yOffset + BackGroupHeight1) / BackGroupHeight1;
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor whiteColor] colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    //  开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext() ;
    
    return theImage;
}
#pragma mark - 数据解析(主播数据)
- (void)dataHanger{
    
    NSString *path = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?position=2&toUid=%@&device=iphone&title=%@", _uid, _titleStr];
    
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        [self.model setValuesForKeysWithDictionary:responseObject];
        
        [self createImageV];
    } filed:^(NSError *error) {
        
        NSLog(@"数据解析失败");
        
    }];
}
// 作品的数据解析
- (void)dataHanderTableV{
    NSString *path = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=iPhone",self.uid];
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"list"];
        if (_isUpStata == YES) {
            
            self.modelArr = [ListModel modelHandleWithArray:arr];
        }else{
            
            for (NSDictionary *dic in arr) {
                ListModel *model = [[ListModel alloc] init];;
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArr addObject:model];
            }
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    } filed:^(NSError *error) {
        
    }];
    
}
/**< 上拉加载 */
- (void)addFooter{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 标记为上拉
        _isUpStata = NO;
        
        [self dataHanderTableV];
        
    }];
    
    [self.tableView.mj_footer beginRefreshing];
}


- (void)viewWillDisappear:(BOOL)animated{
    // 显示navagationbar下面的像素黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                
                UIImageView *imageView = (UIImageView *)obj;
                imageView.hidden = NO;
            }
        }
    }
}

- (void)viewWillAppear:(BOOL)animated{
    // 取消navagationbar下面的像素黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                
                UIImageView *imageView = (UIImageView *)obj;
                imageView.hidden = YES;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

